<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        body {
            margin: 0;
            padding: 0;
            overflow: scroll;
        }

        .map {
            margin: 50px 0;
            width: 852px;
            height: 600px;
            background: url('./bg') no-repeat;
            background-size: 852px;
            overflow: hidden;
            border: 2px solid #000;
            /* position: relative; */
        }

        .card_column {
            position: relative;
            padding: 47px 0 0 11px;
            margin: 0 0 0 10px;
            width: 446px;
            height: 87px;
            background: url('./bg2') no-repeat;
            background-size: 360px;
            box-sizing: border-box;
            z-index: 2;
        }

        .card_column div {
            width: 40px;
            text-align: center;
            color: #000;
            font-size: 16px;
            font-weight: 700;
        }

        .sun_light {
            position: absolute;
            width: 78px;
            height: 78px;
            background: url('./s');
            cursor: pointer;
            z-index: 1;
            opacity: 1;
        }

        .plant_card1 {
            position: absolute;
            height: 56px;
            top: 7px;
            left: 62px;
            background: url('0.png') no-repeat;
            background-size: 40px 56px;
            cursor: pointer;
        }

        .plant_card2 {
            position: absolute;
            height: 56px;
            background: url('2.png') no-repeat;
            top: 8px;
            left: 104px;
            background-size: 39px 54px;
            cursor: pointer;
        }

        .plant1 {
            position: absolute;
            width: 71px;
            height: 71px;
            background-image: url('./1.jpg');
            background-repeat: no-repeat;
            background-size: contain;
        }

        .plant2 {
            position: absolute;
            width: 71px;
            height: 71px;
            background-image: url('./2.gif');
            background-repeat: no-repeat;
            background-size: contain;
        }

        .map_cube {
            position: absolute;
            width: 80px;
            height: 90px;
            /* background-color: rgba(0, 0, 0, 0.4); */
            box-sizing: border-box;
        }

        @keyframes shark {
            0% {
                color: red;
            }

            20% {
                color: #000;
            }

            40% {
                color: red;
            }

            60% {
                color: #000;
            }

            80% {
                color: red;
            }

            100% {
                color: #000;
            }
        }

        .mask {
            position: absolute;
            width: 100%;
            height: 100%;
            background-color: rgba(0, 0, 0, 0.4);
            text-align: center;
            line-height: 56px;
            font-size: 20px;
            font-weight: 700;
            color: red !important;
        }

        @keyframes recover {
            0% {
                height: 100%;
            }

            20% {
                height: 80%;
            }

            40% {
                height: 60%;
            }

            60% {
                height: 40%;
            }

            80% {
                height: 20%;
            }

            100% {
                height: 0;
            }
        }

        .bean {
            position: absolute;
            width: 28px;
            height: 28px;
            /* background: url('http://photogz.photo.store.qq.com/psc?/V1403P1d2rNSdL/ruAMsa53pVQWN7FLK88i5pbTAZ4zc.KYJRMR9C8t70wWkWfPto0OjrC7Ti50nIVCGNswQJhcXew0.P4bhFaYTiDBytrCFdU*nMDaGkfV8Aw!/b&bo=HAAcABwAHAADFzI!&rf=viewer_4'); */
            background-image: url('./1.png');
            background-repeat: no-repeat;
            background-size: contain;
        }

        .zom {
            /* 85, 190, 295, 380, 485*/
            position: absolute;
            left: 860px;
            width: 90px;
            height: 122px;
            /* border: 1px solid #000; */
            background-image: url('./3.gif');
            background-repeat: no-repeat;
            background-size: contain;
            opacity: 1;
            transform: rotateY(180deg);
        }

        @keyframes change {
            0% {
                background-position: 0px 0;
            }

            100% {
                background-position: -96px 0;
            }
        }

        .break {
            position: absolute;
            width: 24px;
            height: 24px;
            background: url('./b');
            animation: change 0.2s steps(4);
            z-index: 89;
        }

        @keyframes death {
            0% {
                opacity: 1;
            }

            100% {
                opacity: 0;
            }
        }
    </style>
</head>

<body>
    <div class="map">
        <div class="card_column">
            <div>50</div>
            <div class="plant_card1" onclick="clickCard(1)"></div>
            <div class="plant_card2" onclick="clickCard(2)"></div>
        </div>
    </div>
    <script>
        //初始化
        //#region
        let map = document.querySelector('.map')
        function init() {
            let initLeft = 40
            let initTop = 135
            for (let i = 0; i < 5; i++) {
                for (let j = 0; j < 9; j++) {
                    let cube = document.createElement('div')
                    cube.className = 'map_cube'
                    cube.style.left = initLeft + 'px'
                    cube.style.top = initTop + 'px'
                    map.appendChild(cube)
                    initLeft += 82
                }
                initLeft = 40
                initTop += 100
            }
        }
        init()
        //#endregion

        //阳光
        //#region 
        //随机数种子
        function getRndInteger(min, max) {
            return Math.floor(Math.random() * (max - min + 1)) + min;
        }
        let sunNumber = document.querySelector('.card_column').querySelector('div')
        setInterval(() => {
            createSunlight()
        }, 6000);
        //自动生产阳光
        function createSunlight() {
            let sunLight = document.createElement('div')
            sunLight.className = 'sun_light'
            sunLight.style.top = 50 + 'px'
            sunLight.style.left = getRndInteger(40, 640) + 'px'
            map.appendChild(sunLight)
            moveSunlight(sunLight)
            sunLight.onclick = function (e) {
                clearInterval(e.target.timer)
                e.target.style.zIndex = 3
                e.target.style.transition = '1s'
                e.target.style.left = '0'
                e.target.style.top = '50px'
                setTimeout(() => {
                    sunNumber.innerHTML = parseInt(sunNumber.innerHTML) + 25
                    map.removeChild(e.target)
                }, 900);
            }
        }
        //阳光自动消失
        function removeSunLight(obj) {
            setTimeout(() => {
                map.removeChild(obj)
            }, 10000)
        }
        //阳光下落
        function moveSunlight(obj) {
            obj.timer = setInterval(() => {
                if (obj.offsetTop >= 450) {
                    clearInterval(obj.timer)
                    removeSunLight(obj)
                }
                obj.style.top = obj.offsetTop + 6 + 'px'
            }, 100);
        }
        //#endregion

        //植物卡片
        //#region
        let map_cubes = document.querySelectorAll('.map_cube')
        let plant_card = document.querySelectorAll('[class^=plant_card]')
        //冷却时间
        let second = [6, 0]
        function createSunLight(obj) {
            setTimeout(() => {
                let sun = document.createElement('div')
                sun.className = 'sun_light'
                sun.style.left = obj.offsetLeft + 'px'
                sun.style.top = obj.offsetTop + 'px'
                map.appendChild(sun)
                sun.onclick = function (e) {
                    clearInterval(e.target.timer)
                    e.target.style.zIndex = 3
                    e.target.style.transition = '1s'
                    e.target.style.left = '0'
                    e.target.style.top = '50px'
                    setTimeout(() => {
                        sunNumber.innerHTML = parseInt(sunNumber.innerHTML) + 25
                        map.removeChild(e.target)
                    }, 900);
                }
                removeSunLight(sun)
            }, getRndInteger(3, 10) * 1000);
            obj.createSunLightTimer = setInterval(() => {
                let sun = document.createElement('div')
                sun.className = 'sun_light'
                sun.style.left = obj.offsetLeft + 'px'
                sun.style.top = obj.offsetTop + 'px'
                map.appendChild(sun)
                sun.onclick = function (e) {
                    clearInterval(e.target.timer)
                    e.target.style.zIndex = 3
                    e.target.style.transition = '1s'
                    e.target.style.left = '0'
                    e.target.style.top = '50px'
                    setTimeout(() => {
                        sunNumber.innerHTML = parseInt(sunNumber.innerHTML) + 25
                        map.removeChild(e.target)
                    }, 900);
                }
                removeSunLight(sun)
            }, 24000);

        }

        function createPlant(flag, needSunNumber) {
            if (plant_card[flag - 1].isMask !== true) {
                if (parseInt(sunNumber.innerHTML) >= needSunNumber) {
                    let plant = document.createElement('div')
                    plant.className = `plant${flag}`
                    //没放置不能攻击,且可以移动
                    plant.isAttack = false
                    map.appendChild(plant)
                    map.onmousemove = function (e) {
                        if (!plant.isAttack) {
                            plant.style.left = e.pageX - 20 + 'px'
                            plant.style.top = e.pageY - 30 + 'px'
                            for (let i = 0; i < map_cubes.length; i++) {
                                map_cubes[i].style.backgroundColor = ''
                                if (e.pageX > map_cubes[i].offsetLeft && e.pageX < map_cubes[i].offsetLeft + 85 && e.pageY > map_cubes[i].offsetTop && e.pageY < map_cubes[i].offsetTop + 90 && map_cubes[i].hasPlant !== true) {
                                    map_cubes[i].style.backgroundColor = 'rgba(0,0,0,0.4)'
                                }
                            }
                        }
                    }
                    //放下卡片
                    document.body.onkeydown = function (e) {
                        if (!plant.isAttack) {
                            if (e.keyCode === 32) {
                                map.removeChild(plant)
                            }
                        }
                    }
                    //放置
                    plant.onclick = function (e) {
                        for (let i = 0; i < map_cubes.length; i++) {
                            if (e.pageX > map_cubes[i].offsetLeft && e.pageX < map_cubes[i].offsetLeft + 85 && e.pageY > map_cubes[i].offsetTop && e.pageY < map_cubes[i].offsetTop + 90 && map_cubes[i].hasPlant !== true) {
                                map_cubes[i].style.backgroundColor = ''
                                plant.isAttack = true
                                plant.isHaveZom = false
                                plant.style.left = map_cubes[i].offsetLeft + 10 + 'px'
                                plant.style.top = map_cubes[i].offsetTop + 7 + 'px'
                                //此位置已有植物
                                map_cubes[i].hasPlant = true
                                sunNumber.innerHTML = parseInt(sunNumber.innerHTML) - needSunNumber
                                let mask = document.createElement('div')
                                mask.className = 'mask'
                                mask.innerHTML = second[flag - 1]
                                plant_card[flag - 1].appendChild(mask)
                                plant_card[flag - 1].isMask = true
                                mask.style.animation = `recover ${second[flag - 1]}s linear`
                                mask.recoverTimer = setInterval(() => {
                                    mask.innerHTML = parseInt(mask.innerHTML) - 1
                                }, 1000);
                                setTimeout(() => {
                                    plant_card[flag - 1].isMask = false
                                    plant_card[flag - 1].removeChild(mask)
                                    clearInterval(mask.recoverTimer)
                                }, second[flag - 1] * 1000);
                                
                                //豌豆射手
                                if (flag === 1) {
                                    createBean(plant)
                                }
                                //向日葵
                                if (flag === 2) {
                                    createSunLight(plant)
                                }
                            }
                        }
                    }
                }
                else {
                    sunNumber.style.animation = 'shark 1s linear'
                    setTimeout(() => {
                        sunNumber.style.animation = ''
                    }, 1000);
                }
            }
        }

        //选择卡片
        function clickCard(flag) {
            switch (flag) {
                case 1:
                    createPlant(flag, 100)
                    break;
                case 2:
                    createPlant(flag, 50)
                    break;
            }
        }

        //#endregion

        //创造子弹
        //#region 
        let zom_s
        let plants
        function createBean(obj) {
            obj.createBeanTimer = setInterval(() => {
                if (obj.isHaveZom === true) {
                    obj.style['background-image'] = "url('./1.gif')";
                    let bean = document.createElement('div')
                    bean.className = 'bean'
                    bean.style.left = obj.offsetLeft + 50 + 'px'
                    bean.style.top = obj.offsetTop + 'px'
                    map.appendChild(bean)
                    moveBean(bean)
                }
                else {
                    obj.style['background-image'] = "url('./1.jpg')";
                }
            }, 1400);
        }

        setInterval(() => {
            let arr = [0, 0, 0, 0, 0]
            zom_s = document.querySelectorAll('.zom')
            plants = document.querySelectorAll('.plant1')
            for (let i = 0; i < zom_s.length; i++) {
                if (zom_s[i].style.top === '85px') {
                    arr[0]++
                }
                if (zom_s[i].style.top === '190px') {
                    arr[1]++
                }
                if (zom_s[i].style.top === '295px') {
                    arr[2]++
                }
                if (zom_s[i].style.top === '380px') {
                    arr[3]++
                }
                if (zom_s[i].style.top === '485px') {
                    arr[4]++
                }
            }
            for (let i = 0; i < zom_s.length; i++) {
                for (let j = 0; j < plants.length; j++) {
                    if (zom_s[i].style.top === '85px' && plants[j].offsetTop === 142) {
                        plants[j].isHaveZom = true
                    }
                    if (zom_s[i].style.top === '190px' && plants[j].offsetTop === 242) {
                        plants[j].isHaveZom = true
                    }
                    if (zom_s[i].style.top === '295px' && plants[j].offsetTop === 342) {
                        plants[j].isHaveZom = true
                    }
                    if (zom_s[i].style.top === '380px' && plants[j].offsetTop === 442) {
                        plants[j].isHaveZom = true
                    }
                    if (zom_s[i].style.top === '485px' && plants[j].offsetTop === 542) {
                        plants[j].isHaveZom = true
                    }
                }
            }
            for (let j = 0; j < plants.length; j++) {
                if (arr[0] === 0 && plants[j].offsetTop === 142) {
                    plants[j].isHaveZom = false
                }
                if (arr[1] === 0 && plants[j].offsetTop === 242) {
                    plants[j].isHaveZom = false
                }
                if (arr[2] === 0 && plants[j].offsetTop === 342) {
                    plants[j].isHaveZom = false
                }
                if (arr[3] === 0 && plants[j].offsetTop === 442) {
                    plants[j].isHaveZom = false
                }
                if (arr[4] === 0 && plants[j].offsetTop === 542) {
                    plants[j].isHaveZom = false
                }
            }
        }, 100);

        //子弹移动
        function moveBean(obj) {
            obj.moveBeanTimer = setInterval(() => {
                obj.style.left = obj.offsetLeft + 4 + 'px'
                if (obj.offsetLeft >= 1160) {
                    clearInterval(obj.moveBeanTimer)
                    obj.remove()
                }
            }, 10);
        }
        //#endregion

        //创造僵尸
        //#region 
        //142 242...
        let pos = [85, 190, 295, 380, 485]
        function createZom() {
            let zom = document.createElement('div')
            let num = getRndInteger(0, 4)
            zom.blood = 10
            zom.className = 'zom'
            zom.style.top = pos[num] + 'px'
            zom.style.zIndex = num
            map.appendChild(zom)
            moveZom(zom)
        }

        //僵尸移动
        function moveZom(obj) {
            obj.moveZomTimer = setInterval(() => {
                obj.style.left = obj.offsetLeft - 1 + 'px'
                if (obj.offsetLeft <= 0) {
                    clearInterval(obj.moveZomTimer)
                    clearInterval(obj.isImpact)
                    obj.style['background-image'] = "url(./4.gif)"
                    obj.style.animation = 'death 5s linear'
                    setTimeout(() => {
                        obj.style.animation = ''
                        obj.remove()
                    }, 5000);
                }
            }, 80);
            obj.isImpact = setInterval(() => {
                isImpact(obj)
            }, 10);
        }

        setInterval(() => {
            createZom()
        }, 18000);

        //#endregion

        //碰撞检测
        let beans
        function isImpact(obj) {
            beans = document.querySelectorAll('.bean')
            for (let i = 0; i < beans.length; i++) {
                if (beans[i].offsetLeft >= obj.offsetLeft) {
                    if (beans[i].offsetTop === 142 && obj.offsetTop === 85
                        || beans[i].offsetTop === 242 && obj.offsetTop === 190
                        || beans[i].offsetTop === 342 && obj.offsetTop === 295
                        || beans[i].offsetTop === 442 && obj.offsetTop === 380
                        || beans[i].offsetTop === 542 && obj.offsetTop === 485) {
                        let boom = document.createElement('div');
                        boom.className = 'break';
                        boom.style.left = beans[i].offsetLeft + 'px';
                        boom.style.top = beans[i].offsetTop + 'px';
                        map.replaceChild(boom, beans[i])
                        setTimeout(() => {
                            map.removeChild(boom)
                        }, 200);
                        obj.blood--
                    }
                }
                //死亡
                if (obj.blood === 0) {
                    clearInterval(obj.moveZomTimer)
                    clearInterval(obj.isImpact)
                    // let death = document.createElement('div');
                    // death.className = 'death';
                    // death.style.background = ''
                    // death.style.left = obj.offsetLeft - 60 + 'px';
                    // death.style.top = obj.offsetTop + 'px';
                    // map.replaceChild(death, obj)
                    // setTimeout(() => {
                    //     map.removeChild(death)
                    // }, 2000);
                    obj.style.animation = 'death 0.2s linear'
                    setTimeout(() => {
                        obj.style.animation = ''
                        obj.remove()
                    }, 200);
                }
            }
        }

    </script>
</body>

</html>